package com.itheima.service;


import com.itheima.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴
 * @version V1.0
 * @Description 使用freemarker生成静态页面
 * @Package com.itheima.service.Impl
 * @date 2021/11/30 10:44
 */
@Component
@Transactional
public class GenerateStaticHtml {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freePath}")
    private String freePath;

    /**
     * 生成静态页面 , 用户在对套餐数据执行增删改操作的时候执行
     */
    public void generateMobileStaticHtml() {
        //准备模板文件中所需的数据
        List<Setmeal> setmealList = setMealService.findSetMealAll();
        //生成套餐列表静态页面
        generateMobileSetmealListHtml(setmealList);
        //生成套餐详情静态页面（多个）
        generateMobileSetmealDetailHtml(setmealList);
    }

    /**
     * 生成套餐列表静态页面
     *
     * @param setmealList  全部的套餐信息
     */
    private void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("setmealList", setmealList);

        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out = null;
        try {
            // 加载模版文件
            Template template = configuration.getTemplate("setmeal.ftl");

            // 生成数据
            File docFile = new File(freePath + "\\setmeal.html");
            out = new BufferedWriter(new FileWriter(docFile));
            // 输出文件
            template.process(dataMap, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 生成套餐详情静态页面（多个）
     *
     * @param setmealList
     */
    private void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {
        for (Setmeal setmeal : setmealList) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            //查询体检套餐详情
            dataMap.put("setmeal", setMealService.findCheckGroupInfoById(setmeal.getId()));

            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Writer out = null;
            try {
                // 加载模版文件
                Template template = configuration.getTemplate("setmeal_detail.ftl");

                // 生成数据
                File docFile = new File(freePath + "\\" + "setmeal_detail_" + setmeal.getId() + ".html");
                out = new BufferedWriter(new FileWriter(docFile));
                // 输出文件
                template.process(dataMap, out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != out) {
                        out.flush();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }



}
